package com.yun.interceptor;

import com.yun.utils.UserUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		String url = request.getRequestURI();
		if (url.contains("login.action") || url.contains("register.action") || url.contains("share.action")
				|| url.contains("getShareFiles.action") || url.contains("download.action")
				|| url.contains("loginForApp.action") || url.contains("getAppFiles.action")
				|| url.contains("uploadForApp.action")) {
			return true;
		}

		String username = UserUtils.getUsername(request);

		if (username != null) {
			return true;
		}

		response.sendRedirect("/SharedCloud_war_exploded/user/login.action");
		return false;
	}

}
